﻿using Microsoft.Extensions.Caching.Memory;
using System.Data;
using System.Data.Common;
using System.Runtime.Caching;
//using static DevExpress.Utils.SafeXml;

namespace ztymodbus
{
    public class MyMemoryCache
    {
        //public IMemoryCache _cache;

        ////静态类不能有实例构造函数。可以使用静态方法来初始化缓存。
        //public MyMemoryCache(IMemoryCache memoryCache)
        //{
        //    _cache = memoryCache;
        //}

        ObjectCache _cache = System.Runtime.Caching.MemoryCache.Default;
        Code code = new Code();
        public DataTable get_staff()
        {
            
            string cacheKey = "Staff";
            DataSet dataSet = new DataSet();
            // 尝试从缓存中获取 DataSet
            //if (_cache.TryGetValue(cacheKey, out dataSet))
            if(_cache.Contains(cacheKey))
            {

                dataSet = (DataSet)_cache.Get(cacheKey);
                // 如果缓存中存在
                //Console.WriteLine("从缓存中获取");
                //code.Into_Blog("MyMemoryCache 从缓存中获取", 1, "Staff",1);
            }
            else
            {
                //Console.WriteLine("从数据库获取");
                //code.Into_Blog("MyMemoryCache 从数据库获取Staff", 1, "Staff",1);
                // 如果缓存中不存在，从数据库查询
                //dataSet = new DataSet();
                using (DbConnection con = new SqlConnection(Conn.ConnString()))
                {
                    con.Open();
                    using (DbCommand com = con.CreateCommand()) // new SqlCommand(sql, con)
                    {
                        string sql = "select * from [Staff] order by id desc";
                        com.CommandText = sql;
                        using (DbDataAdapter sda = new SqlDataAdapter((SqlCommand)com))  // new SqlDataAdapter(com)
                        {
                            using (dataSet = new DataSet("Staff"))
                            {
                                sda.Fill(dataSet);
                            }
                            com.Parameters.Clear();
                        }
                    }

                }

                // 设置缓存选项
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                    .SetSlidingExpiration(TimeSpan.FromMinutes(30));  //超过30分钟未使用，则清除。

                // 创建缓存策略，这里使用绝对过期
                TimeSpan cacheDuration = TimeSpan.FromMinutes(60);
                DateTimeOffset dateTimeOffset = DateTimeOffset.Now.Add(cacheDuration);
                // 将 SlidingExpiration 的赋值从 dateTimeOffset (DateTimeOffset 类型) 改为正确的 TimeSpan 类型
                CacheItemPolicy policy = new CacheItemPolicy
                {
                    SlidingExpiration = cacheDuration // cacheDuration 是 TimeSpan 类型
                };
                // 将 DataSet 存入缓存
                _cache.Add(cacheKey, dataSet ,policy);
            }

            // 获取 DataTable
            DataTable productsTable = dataSet.Tables[0];

            // 使用 DataTable.Select 方法进行查询
            // 注意：这里的查询语法类似于 SQL 的 WHERE 子句
            //DataRow[] selectedRows = productsTable.Select("Staff_Status = 100 AND ID =1 ");

            //foreach (var row in selectedRows)
            //{
            //    Console.WriteLine(row["Staff_Name"].ToString());
            //}


            return productsTable;
        }

        //更新Staff缓存
        public void setCache_Staff()
        {

            string cacheKey = "Staff";
            DataSet dataSet = new DataSet();
            using (DbConnection con = new SqlConnection(Conn.ConnString()))
            {
                con.Open();
                using (DbCommand com = con.CreateCommand()) // new SqlCommand(sql, con)
                {
                    string sql = "select * from [Staff] order by id desc";
                    com.CommandText = sql;
                    using (DbDataAdapter sda = new SqlDataAdapter((SqlCommand)com))  // new SqlDataAdapter(com)
                    {
                        using (dataSet = new DataSet("Staff"))
                        {
                            sda.Fill(dataSet);
                        }
                        com.Parameters.Clear();
                    }
                }

            }

          
            // 创建缓存策略，这里使用相对过期
            TimeSpan cacheDuration = TimeSpan.FromMinutes(60);
            DateTimeOffset dateTimeOffset = DateTimeOffset.Now.Add(cacheDuration);
            CacheItemPolicy policy = new CacheItemPolicy
            {
                SlidingExpiration = cacheDuration // cacheDuration 是 TimeSpan 类型
            };
            // 将 DataSet 存入缓存
            _cache.Remove(cacheKey);
            _cache.Add(cacheKey, dataSet, policy);
        }

        //更新config表缓存
        public void setCache_Config()
        {

            string cacheKey = "config";
            DataSet dataSet = new DataSet();
            using (DbConnection con = new SqlConnection(Conn.ConnString()))
            {
                con.Open();
                using (DbCommand com = con.CreateCommand()) // new SqlCommand(sql, con)
                {
                    string sql = "select * from [config] order by id desc";
                    com.CommandText = sql;
                    using (DbDataAdapter sda = new SqlDataAdapter((SqlCommand)com))  // new SqlDataAdapter(com)
                    {
                        using (dataSet = new DataSet(cacheKey))
                        {
                            sda.Fill(dataSet);
                        }
                        com.Parameters.Clear();
                    }
                }

            }

            // 创建缓存策略，这里使用相对过期
            TimeSpan cacheDuration = TimeSpan.FromMinutes(60);
            DateTimeOffset dateTimeOffset = DateTimeOffset.Now.Add(cacheDuration);
            CacheItemPolicy policy = new CacheItemPolicy
            {
                SlidingExpiration = cacheDuration // cacheDuration 是 TimeSpan 类型
            };
            // 将 DataSet 存入缓存
            _cache.Remove(cacheKey);
            _cache.Add(cacheKey, dataSet, policy);

        }



        }
}
